$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'integral/list',
        datatype: "json",
        colModel: [			
			{ label: 'dataId',hidden:true, name: 'dataId', width: 50, key: true },
			{ label: '经营主体', name: 'subjectName',width: 80 }, 		
			{ label: '获取类型', name: 'integralType', width: 80, formatter: function(value, options, row){
				return value === 0 ? 
					'<span>登陆获取</span>' : 
					'<span>做任务获取</span>';
			}}, 			
			{ label: '积分数量', name: 'integralcount',width: 80 }, 			
			{ label: '描述', name: 'description',width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }			
        ],
		viewrecords: true,
        height: (pageInfo().height-130),
        rowNum: 15,
		rowList : [15,50,99],
        loadtext : '<img src="../../statics/plugins/layer/skin/default/loading-1.gif" style="margin-right:10px;"/>正在加载,请稍候……',
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        loadComplete : function(data){
            if(data.code == 0){
                return data;
            }else if(data.code == 205){
                invalidAccess();
            }else{
                layer.alert(data.msg);
            }
        },
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;
var vm = new Vue({
	el:'#witapp',
	data:{
		showList: true,
		title: null,
		integral: {
			status:1,
			deptId:null,
            deptName:null
		},
		q:{
            key: null
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.integral = {status:1,deptId:null, deptName:null};
			vm.getDept();
		},
		update: function (event) {
			var dataId = getSelectedRow();
			if(dataId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(dataId)
		},
		saveOrUpdate: function (event) {
			var url = vm.integral.dataId == null ? "integral/save" : "integral/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.integral),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var dataIds = getSelectedRows();
			if(dataIds == null){
				return ;
			}
            confirm('确定要删除选中的记录？', function(){
                ajaxExecute(baseURL + "integral/delete",JSON.stringify(dataIds),function(data){
                    if(data.code == 0){
                        $("#jqGrid").trigger("reloadGrid");
                    }else{
                        alert(data.msg);
                    }
                });
            });
		},
		getInfo: function(dataId){
			$.get(baseURL + "integral/info/"+dataId, function(r){
                vm.integral = r.integral;
                vm.getDept();
            });
		},
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.integral.deptId = node[0].deptId;
                    vm.integral.deptName = node[0].name;

                    layer.close(index);
                }
            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
		},
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.integral.deptId);
                if(node != null){
                    ztree.selectNode(node);

                    vm.integral.deptName = node.name;
                }
            })
        }
	}
});